package imagecutter.amra;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

public class Configuration {

    Properties properties;

    public static final String IMAGES_VERTICAL = "images.vertical";
    public static final String IMAGES_HORIZONTAL = "images.horizontal";
    public static final String SOURCE = "source";


    /**
     * Loads configuration from a file.
     * @param filename
     * @throws IOException
     */
    public Configuration(String filename) throws IOException, Exception{
        Properties prop = new Properties();
        prop.load(new FileReader(filename));
        init(prop);
    }

    /**
     * Loads configuration from properties object
     * @param properties
     */
    public Configuration(Properties properties) throws Exception{
        init(properties);
    }
    
    private void init(Properties properties) throws Exception{
        this.properties = properties;
        configCheck();
    }

    private void configCheck() throws Exception{
        if(!FileOperation.checkFile(getSourceImageFile()))
            throw new Exception("Source image file doesn't exist.");
        if(!properties.containsKey(IMAGES_HORIZONTAL))
            throw new Exception("Configuration key '"+IMAGES_HORIZONTAL+"' not found in configuration file.");
        if(!properties.containsKey(IMAGES_VERTICAL))
            throw new Exception("Configuration key '"+IMAGES_VERTICAL+"' not found in configuration file.");
        try{
           getObjectAsInteger(properties.get(IMAGES_HORIZONTAL));
        }catch(Exception e){
            throw new Exception("Configuration key '"+IMAGES_HORIZONTAL+"' is not a integer: '"+properties.get(IMAGES_HORIZONTAL)+"'.", e);
        }
        try{
           getObjectAsInteger(properties.get(IMAGES_VERTICAL));
        }catch(Exception e){
            throw new Exception("Configuration key '"+IMAGES_VERTICAL+"' is not a integer: '"+properties.get(IMAGES_HORIZONTAL)+"'.", e);
        }
    }

    /**
     * Converts object to integer or throws a exception.
     * @param o
     * @return
     */
    private int getObjectAsInteger(Object o){
        return Integer.getInteger(o.toString().trim());
    }

    /**
     * Loads configuration for creating subimages.
     * @return Collection of rectangles. For each rectangle a new image will be created.
     */
    public Collection<Rectangle> loadCutConfiguration(){
        Collection<Rectangle> result = new ArrayList<Rectangle>();
        Rectangle sourceRect = getSourceImageRectangle();
        int imagesVertical = getObjectAsInteger(properties.get(IMAGES_VERTICAL));
        int imagesHorizontal = getObjectAsInteger(properties.get(IMAGES_HORIZONTAL));

        for(int horizontal = 0; horizontal < imagesHorizontal; horizontal++){
            for(int vertical = 0; vertical < imagesVertical; vertical++){
                Rectangle subRectangle = sourceRect.getSubRectangle(imagesVertical, imagesHorizontal, vertical, horizontal);
                result.add(subRectangle);
            }
        }

        return result;
    }

    /**
     * Returns <code>File</code> from which subImages will be created.
     * @return Source image file
     */
    public File getSourceImageFile(){
        return new File(properties.getProperty(SOURCE));
    }

    private Rectangle getSourceImageRectangle(){
        BufferedImage bi = FileOperation.loadImageFromFile(getSourceImageFile());
        return ImageOperation.loadRectangleForImage(bi);
    }

    /**
     * Returns <code>File</code> for a subImage where a new created image will be stored.
     * @return subImage file
     */
    public File getNextSubImageFile(){
        //TODO
        return null;
    }
}
